<template>
  <ex-dialog width="700px" class="ex-diolog" :title="title" :visible="visible" :close-on-click-modal="false"
    @closeDialog="handleClose">
    <div class="main" v-loading="loading">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" :label-width="labelWidth" class="demo-ruleForm">
        <el-form-item label="邀请人姓名：" prop="name" >
          <el-input v-model="ruleForm.name" placeholder="请填写邀请人姓名" :style="{width: inputWidth}"></el-input>
        </el-form-item>
        <el-form-item label="手机号：" prop="phone" >
          <el-input v-model="ruleForm.phone" placeholder="请填写手机号" :style="{width: inputWidth}"></el-input>
        </el-form-item>
        <el-form-item label="邀请码：" prop="referrer" >
          <el-input v-model="ruleForm.referrer" placeholder="请填写邀请码" :style="{width: inputWidth}"></el-input>
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-input v-model="ruleForm.remark" placeholder="请输入" clearable :style="{width: inputWidth}" />
        </el-form-item>
      </el-form>
    </div>
    <div class="button_box">
      <el-button type="primary" :loading="AgreeLoading" @click="submitForm('ruleForm')" v-if="id==null">保存提交</el-button>
      <el-button type="primary" :loading="AgreeLoading" @click="editForm('ruleForm')" v-if="id!=null">保存提交</el-button>
      <el-button @click="handleClose">取消</el-button>
    </div>
  </ex-dialog>
</template>
<script>
  import Loading from "@/components/Loading/index";
  // api
  import {
    addReferrer,
    referrerDetail,
    editReferrer
  } from "@/api/consumeMiningManagement/inviterManagement";

  export default {
    components: {
      Loading,
    },
    props: {
      visible: Boolean,
      id: String,
    },
    data() {
      return {
        labelWidth: '120px',
        inputWidth: '300px',
        title: '新增/编辑邀请人',
        loading: false,
        disabled: false,
        AgreeLoading: false, //按钮loading
        ruleForm: {
          name:'',//行业名称
          phone:'',//手机号
          referrer: '',//邀请码
          remark: '',//备注
        },
        rules: {
          name: [{
            required: true,
            message: "请填写邀请人姓名",
            trigger: ['change', 'bulr'],
          }],
          phone: [{
            required: true,
            message: "请填写手机号",
            trigger: 'bulr',
          },
          // {pattern: /^1[34578]\d{9}$/,message: '目前只支持中国大陆的手机号码'}
        ],
          referrer: [
          {pattern: /\b\d{4}\b/,message: '只能填写4位数整数'},  
          {
            required: true,
            message: "请填写邀请码",
            trigger: 'bulr',
          }
          
        ]
        },
      };
    },
    computed: {},
    created() {
      console.log(this.id)
      if (this.id != null) {
        this.disabled = true
        this.getInfo()
      }

    },
    mounted() {

    },
    methods: {
      //取消
      handleClose(formName) {
        this.$emit("update:visible", false);
      },
      getInfo() { //获取信息详情
        this.loading = true
        referrerDetail(this.id).then((res) => {
            this.loading = false
            this.ruleForm = res.result
          })
          .finally(() => {
            this.loading = false;
          });
      },
      submitForm(formName) {
        //创建确定
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.AgreeLoading = true
            var params = {
              ...this.ruleForm
            };
            addReferrer(params).then((res) => {
                this.$message({
                  message: "保存成功",
                  type: "success",
                });
                setTimeout(() => {
                  this.$emit('success')
                  this.handleClose()
                  this.AgreeLoading = false
                }, 1000);
              })
              .catch(() => {
                this.AgreeLoading = false;
              });

          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      editForm(formName) {
        //编辑确定
        this.$refs[formName].validate((valid) => {
          if (valid) {
            //   alert("submit!");editGroupon
            this.AgreeLoading = true
            var params = {
              id: this.id,
              ...this.ruleForm,
            };
            editReferrer(params).then((res) => {
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                setTimeout(() => {
                  this.$emit('success')
                  this.handleClose()
                  this.AgreeLoading = false
                }, 1000);
              })
              .catch(() => {
                this.AgreeLoading = false;
              });

          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
    },
  };

</script>
<style lang="scss" scoped>
  .ex-diolog {
    ::v-deep .ex--dialog--footer {
      border: none !important;
      padding: 0 !important;
    }

    ::v-deep .ex--dialog--main {
      padding: 10px 20px !important;
    }
  }

  .main {
    padding: 20px 20px;
  }

  .from_box {
    display: flex;
    margin-bottom: 20px;
  }

  .from_box_l {
    width: 110px;
    color: #606266;
    font-size: 14px;
    font-weight: bold;
  }

  .from_box_r {
    color: #333;
    font-size: 15px;
    flex: 1;
  }

  .el_table_box {
    padding: 0 20px 40px;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    margin-bottom: 20px;
  }

  .el_table_box_h {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 0;
    border-bottom: 1px solid #f3f3f3;
    margin-bottom: 14px;
  }

  .el_table_box_h_title {
    border-left: 4px solid #ff9300;
    padding-left: 4px;
  }

  .carousel_box {
    width: 400px;
    margin-bottom: 20px;
  }

  .swiper_img {
    width: 100%;
    height: 100%;
  }

  .carousel_box_two {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
  }

  .phone_box {
    width: 100px;
    height: 100px;
    margin: 0 10px 10px 0;
  }

  .button_box {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px 0;
  }

  ::v-deep .avatar-uploader2 .el-upload {
    width: 100% !important;
  }

  .avatar-uploader2 {
    width: 228px;
    height: 128px;
    text-align: center;
    line-height: 128px;
    background-color: #fbfdff;
    border: 1px dashed #c0ccda;
  }

  .banner_img {
    width: 228px;
    height: 128px;
  }

  .richText {
    border: 1px solid #ccc;
    line-height: normal;
  }

</style>
